package com.wiken.orderservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wiken.orderservice.entity.Order;
import com.wiken.orderservice.service.impl.OrderServiceImpl;
import com.wiken.orderservice.vo.OrderAndCommmodityVO;
import com.wiken.orderservice.vo.OrderCommodityListVO;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.PageUtill;
import com.wiken.servicecommon.common.R;
import com.wiken.servicecommon.common.exception.DefaultException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@RestController
@RequestMapping("/orderservice/order")
public class OrderController {

    @Autowired
    OrderServiceImpl orderService;
    @PostMapping("addOrder")
    public R addOrder(@RequestBody OrderCommodityListVO orderCommodityListVO,
                      @RequestHeader("Authorization") String token){

        Order order = orderService.addOrder(orderCommodityListVO, orderCommodityListVO.getOrderCommodityList(), token);

        if(order == null) throw new DefaultException(Code.ERROR_ORDER_COMMODITY_INSERT, "添加订单失败");

        return R.ok().code(Code.SUCCESS).message("添加订单成功").data("order", order);
    }

    @PostMapping("delOrder")
    public R delOrder(@RequestBody String orderId, @RequestHeader("Authorization") String token){
        boolean success = orderService.delOrder(orderId, token);

        if(!success) return R.error().code(Code.ERROR).message("订单删除失败");

        return R.ok().code(Code.SUCCESS).message("订单删除成功");
    }

    @PostMapping("getOrderOfPayed")
    public R getOrderOfPayed(@RequestBody PageUtill pageUtill, @RequestHeader("Authorization") String token){
        Page<OrderAndCommmodityVO> orderList = orderService.getOrderOfPayed(token, pageUtill);

        return R.ok().code(Code.SUCCESS).message("获取订单成功").data("orderList", orderList);
    }

    @PostMapping("getOrderOfPay")
    public R getOrderOfPay(@RequestBody PageUtill pageUtill, @RequestHeader("Authorization") String token){
        Page<OrderAndCommmodityVO> orderList = orderService.getOrderOfPay(token, pageUtill);

        return R.ok().code(Code.SUCCESS).message("获取订单成功").data("orderList", orderList);
    }

    @PostMapping("getOrderOfExpire")
    public R getOrderOfExpire(@RequestBody PageUtill pageUtill, @RequestHeader("Authorization") String token){
        Page<OrderAndCommmodityVO> orderList = orderService.getOrderOfExpire(token, pageUtill);

        return R.ok().code(Code.SUCCESS).message("获取订单成功").data("orderList", orderList);
    }
}
